local keymap = vim.keymap
local opts = {
  noremap = true,
  silent = true,
}

-- 插入模式
-- Ctrl-s保存
keymap.set("i", "<C-s>", "<Esc>:w<CR>i")

-- 普通模式
-- 打开Telescope（前缀键+Shift+f）
keymap.set("n", "<leader><S-f>", ":Telescope<CR>", opts)
-- 打开NvimTree（前缀键+Shift+E）
keymap.set("n", "<leader><S-e>", ":NvimTreeToggle<CR>", opts)
-- 打开Neogit（前缀键+Shift+G)
keymap.set("n", "<leader><S-g>", ":Neogit<CR>", opts)
-- 打开toggleterm（Ctrl+J）
keymap.set("n", "<C-j>", ":ToggleTerm<CR>", opts)
-- 打开outine（Ctrl+Alt+b）
keymap.set("n", "<C-A-b>", ":Outline<CR>", opts)
-- 切换到后一个标签（Ctrl+PageUp）
keymap.set("n", "<C-PageDown>", ":BufferLineCycleNext<CR>", opts)
-- 切换到前一个标签（Ctrl+PageDown）
keymap.set("n", "<C-PageUp>", ":BufferLineCyclePrev<CR>", opts)
-- 关闭标签（前缀键+F4）
keymap.set("n", "<leader><F4>", ":bd<CR>", opts)
-- 窗口跳转（Alt+hjkl）
keymap.set("n", "<A-h>", "<C-w>h", opts)
keymap.set("n", "<A-j>", "<C-w>j", opts)
keymap.set("n", "<A-k>", "<C-w>k", opts)
keymap.set("n", "<A-l>", "<C-w>l", opts)
-- 窗口大小调整（Shift+方向键）
keymap.set("n", "<S-Up>", ":resize -1<CR>", opts)
keymap.set("n", "<S-Down>", ":resize +1<CR>", opts)
keymap.set("n", "<S-Left>", ":vertical resize +1<CR>", opts)
keymap.set("n", "<S-Right>", ":vertical resize -1<CR>", opts)

-- 底部终端快捷键
keymap.set("t", "<esc>", [[<C-\><C-n>]], opts)
keymap.set("t", "jk", [[<C-\><C-n>]], opts)
keymap.set("t", "<C-h>", [[<Cmd>wincmd h<CR>]], opts)
keymap.set("t", "<C-j>", [[<Cmd>wincmd j<CR>]], opts)
keymap.set("t", "<C-k>", [[<Cmd>wincmd k<CR>]], opts)
keymap.set("t", "<C-l>", [[<Cmd>wincmd l<CR>]], opts)
keymap.set("t", "<C-w>", [[<C-\><C-n><C-w>]], opts)

-- Global mappings.
-- See `:help vim.diagnostic.*` for documentation on any of the below functions
keymap.set("n", "<space>e", vim.diagnostic.open_float)
keymap.set("n", "[d", vim.diagnostic.goto_prev)
keymap.set("n", "]d", vim.diagnostic.goto_next)
keymap.set("n", "<space>q", vim.diagnostic.setloclist)

-- Use LspAttach autocommand to only map the following keys
-- after the language server attaches to the current buffer
vim.api.nvim_create_autocmd("LspAttach", {
  group = vim.api.nvim_create_augroup("UserLspConfig", {}),
  callback = function(ev)
    -- Enable completion triggered by <c-x><c-o>
    vim.bo[ev.buf].omnifunc = "v:lua.vim.lsp.omnifunc"

    -- Buffer local mappings.
    -- See `:help vim.lsp.*` for documentation on any of the below functions
    local opts = { buffer = ev.buf }
    keymap.set("n", "gD", vim.lsp.buf.declaration, opts)
    keymap.set("n", "gd", vim.lsp.buf.definition, opts)
    keymap.set("n", "K", vim.lsp.buf.hover, opts)
    keymap.set("n", "gi", vim.lsp.buf.implementation, opts)
    keymap.set("n", "<C-k>", vim.lsp.buf.signature_help, opts)
    keymap.set("n", "<space>wa", vim.lsp.buf.add_workspace_folder, opts)
    keymap.set("n", "<space>wr", vim.lsp.buf.remove_workspace_folder, opts)
    keymap.set("n", "<space>wl", function()
      print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
    end, opts)
    keymap.set("n", "<space>D", vim.lsp.buf.type_definition, opts)
    keymap.set("n", "<F2>", vim.lsp.buf.rename, opts)
    keymap.set({ "n", "v" }, "<space>ca", vim.lsp.buf.code_action, opts)
    keymap.set("n", "gr", vim.lsp.buf.references, opts)
    keymap.set("n", "<S-A-f>", function()
      vim.lsp.buf.format({ async = true })
    end, opts)
  end,
})

-- lsp_signature
-- 显示签名提示
vim.keymap.set({ "n" }, "<C-k>", function()
  require("lsp_signature").toggle_float_win()
end, { silent = true, noremap = true, desc = "toggle signature" })
vim.keymap.set({ "n" }, "<Leader>k", function()
  vim.lsp.buf.signature_help()
end, { silent = true, noremap = true, desc = "toggle signature" })
